package com.ruoyi.project.im.doyuming.controller;

import java.util.List;
import java.util.Random;

import com.ruoyi.project.im.personhost.domain.Personhost;
import com.ruoyi.project.im.personhost.service.IPersonhostService;
import com.ruoyi.project.im.users.domain.Users;
import com.ruoyi.project.im.users.service.IUsersService;
import com.ruoyi.project.im.utils.RandomNum;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.im.doyuming.domain.Doyuming;
import com.ruoyi.project.im.doyuming.service.IDoyumingService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 域名列表Controller
 * 
 * @author ruoyi
 * @date 2022-09-11
 */
@Controller
@RequestMapping("/im/doyuming")
public class DoyumingController extends BaseController
{
    private String prefix = "im/doyuming";

    @Autowired
    private IDoyumingService doyumingService;

    @Autowired
    private IUsersService iUsersService;

    @Autowired
    private IPersonhostService iPersonhostService;

    @RequiresPermissions("im:doyuming:view")
    @GetMapping()
    public String doyuming()
    {
        return prefix + "/doyuming";
    }

    /**
     * 查询域名列表列表
     */
    @RequiresPermissions("im:doyuming:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Doyuming doyuming)
    {
        startPage();
        List<Doyuming> list = doyumingService.selectDoyumingList(doyuming);
        return getDataTable(list);
    }

    /**
     * 导出域名列表列表
     */
    @RequiresPermissions("im:doyuming:export")
    @Log(title = "域名列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Doyuming doyuming)
    {
        List<Doyuming> list = doyumingService.selectDoyumingList(doyuming);
        ExcelUtil<Doyuming> util = new ExcelUtil<Doyuming>(Doyuming.class);
        return util.exportExcel(list, "域名列表数据");
    }

    /**
     * 新增域名列表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存域名列表
     */
    @RequiresPermissions("im:doyuming:add")
    @Log(title = "域名列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Doyuming doyuming)
    {
        return toAjax(doyumingService.insertDoyuming(doyuming));
    }

    /**
     * 修改域名列表
     */
    @RequiresPermissions("im:doyuming:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Doyuming doyuming = doyumingService.selectDoyumingById(id);
        mmap.put("doyuming", doyuming);
        return prefix + "/edit";
    }

    /**
     * 修改保存域名列表
     */
    @RequiresPermissions("im:doyuming:edit")
    @Log(title = "域名列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Doyuming doyuming)
    {
        return toAjax(doyumingService.updateDoyuming(doyuming));
    }



    /**
     * 修改域名列表
     */
    @RequiresPermissions("im:doyuming:edit")
    @GetMapping("/bangding/{id}")
    public String bangding(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("id", id);
        return prefix + "/bangding";
    }

    /**
     * 修改保存域名列表
     */
    @RequiresPermissions("im:doyuming:edit")
    @Log(title = "域名列表", businessType = BusinessType.UPDATE)
    @PostMapping("/bangding")
    @ResponseBody
    public AjaxResult bangdingSave(Long id,String kamicode)
    {

       Doyuming doyuming= doyumingService.selectDoyumingById(id);
       if (doyuming==null){
           return AjaxResult.error("没找到该域名");
       }
       Users users=iUsersService.selectUsersByKamicode(kamicode);
       if (null==users){
           return AjaxResult.error("卡密错误");
       }
       Personhost personhost=new Personhost();
       personhost.setUserId(users.getUserId());
       personhost.setShortcode(RandomNum.randomxiaoxie(4));
       personhost.setLeixing(3l);
       personhost.setUrl("http://"+doyuming.getDomain());
       personhost.setStatus(2l);
       int i =iPersonhostService.insertPersonhost(personhost);
       if(i>0){
           doyuming.setIsUse(1);
           doyuming.setBeizhu(kamicode+"使用");
           doyumingService.updateDoyuming(doyuming);
           return AjaxResult.success("成功");
       }else {
           return AjaxResult.error("失败");
       }

    }
    /**
     * 删除域名列表
     */
    @RequiresPermissions("im:doyuming:remove")
    @Log(title = "域名列表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(doyumingService.deleteDoyumingByIds(ids));
    }

    @PostMapping("/resetDomain")
    @ResponseBody
    public AjaxResult resetDomain(Long id) {
        Doyuming doyuming=doyumingService.selectDoyumingById(id);//查找改域名
        if(doyuming==null){
            return toAjax(false);
        }
        Doyuming newDoyuming=doyumingService.selectDoyumingByZone(doyuming.getLeixing());
        String domain=newDoyuming.getDomain();

        List<Personhost> list=iPersonhostService.selectListByDoyuming(Long.valueOf(doyuming.getLeixing()),doyuming.getDomain());
        for (Personhost personhost : list) {
            personhost.setUrl("http://"+RandomNum.randomxiaoxie(10)+"."+domain);
            iPersonhostService.updatePersonhost(personhost);
        }
        newDoyuming.setFenpeicount(Long.valueOf(list.size()));
        doyumingService.updateDoyuming(newDoyuming);
        return toAjax(true);
    }
}
